package Thread;

public class Demo11 {
//    有三个线程，线程名称分别为：a，b，c。
//    每个线程打印自己的名称。
//    需要让他们同时启动，并按 c，b，a的顺序打印

    private static Object locker = new Object();
    private static int state = 1;

    public static void main(String[] args) throws InterruptedException {
        //通过state这个变量作为判断条件,不满足该条件的线程进入等待,满足条件的线程则先打印,
        //然后改变条件,通过notify唤醒其他线程,继续判断,打印;
        Thread thread1 = new Thread(() -> {
            synchronized (locker) {
                while(state != 1) {
                    try {
                        locker.wait();
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("C");
                state = 2;
                locker.notify();
            }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (locker) {
                while(state != 2) {
                    try {
                        locker.wait();
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("B");
                state = 3;
                locker.notify();
            }
        });

        Thread thread3 = new Thread(() -> {
            synchronized (locker) {
                while(state != 3) {
                    try {
                        locker.wait();
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("A");
                state = 1;
                locker.notify();
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
        thread1.join();
        thread2.join();
        thread3.join();
    }
}

